<template>
<!-- 商品分类 -->
  <div>
    <!-- 面包削区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加分类 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCategoryDialogClick">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table
        :data="categoriesList"
        style="width: 100%;margin-bottom: 20px;margin-top: 15px;"
        row-key="cat_id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
          type="index"
          label="#"
          width="48">
        </el-table-column>
        <el-table-column
          prop="cat_name"
          label="分类名称"
          >
        </el-table-column>
        <el-table-column
          label="是否有效"
          width="180">
          <template slot-scope="scope">
            <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:#90ee90;"></i>
            <i class="el-icon-error" v-else style="color:red;"></i>
          </template>
        </el-table-column>
        <el-table-column
          label="级别">
          <template slot-scope="scope">
            <el-tag  effect="dark" v-if="scope.row.cat_level==0">一级</el-tag>
            <el-tag  effect="dark" type="success" v-else-if="scope.row.cat_level==1">二级</el-tag>
            <el-tag  effect="dark" type="warning" v-else>三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="categoryClcik(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteClick(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramsInfo.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="paramsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 编辑分类对话框 -->
    <el-dialog
      title="编辑分类对话框"
      :visible.sync="categoryDialogVisible"
      width="50%">
      <el-form :model="categoryRuleForm" :rules="categoryRules" ref="categoryRuleForm" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="categoryRuleForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="categoryDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitClick">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类对话框 -->
    <el-dialog
      @close="categoryClose"
      title="添加分类"
      :visible.sync="addCategoryDialogVisible"
      width="50%">
      <el-form :model="addCategoryRuleForm" :rules="addCategoryRules" ref="addCategoryRuleForm" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCategoryRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
            <el-cascader
              clearable
              v-model="selectedKeys"
              :options="parentCategorys"
              :props="parentProps"
              @change="parentCategorysChange">
            </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addCategoryDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategorySubmitClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {CategoriesListData,CompileCategoryData,DeleteCategoryData,addCategoryData} from '@/request/api'
export default {
  name:'Categories',
  data(){
    return{
      //商品列表请求参数
      paramsInfo:{
        type:3,
        pagenum:1,
        pagesize:5
      },
      //商品分类列表
      categoriesList:[],
      //商品总条数
      total:0,
      //商品分类对话框控制
      categoryDialogVisible:false,
      //编辑分类对话框数据
      categoryRuleForm:{
        cat_name:'',
      },
      //商品id
      id:'',
      //验证规则
      categoryRules:{
        cat_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
      },
      //添加分类
      addCategoryDialogVisible:false,
      //添加分类对象
      addCategoryRuleForm:{
        //分类父 ID 不能为空，如果要添加1级分类，则父分类Id应该设置为  `0`
        cat_pid:0,
        //分类名称
        cat_name:'',
        //分类层级不能为空，`0`表示一级分类；`1`表示二级分类；`2`表示三级分类
        cat_level:0
      },
      //添加分类校验规则
      addCategoryRules:{
        cat_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
      },
      //父级分类列表
      parentCategorys:[],
      //父级分类配置
      parentProps:{
        expandTrigger:'hover',
        value:'cat_id',
        label:'cat_name',
        children:'children',
        checkStrictly:true
      },
      //选中的父级分类的id
      selectedKeys:[]
    }
  },
  created(){
    this.categoryHandle(this.paramsInfo)
  },
  methods:{
    //添加分类关闭事件
    categoryClose(){
      this.addCategoryRuleForm = {},
      this.selectedKeys = []
      this.addCategoryRuleForm.cat_pid = 0
      this.addCategoryRuleForm.cat_level = 0
    },
    //添加分类按钮
    addCategoryDialogClick(){
      CategoriesListData({
        type:2
      }).then(res=>{
        if(res.meta.status==200){
          this.parentCategorys = res.data
          console.log(this.parentCategorys);
        }
      })

      this.addCategoryDialogVisible=true
    },
    //
    parentCategorysChange(){
      // console.log(val);
      //
      if(this.selectedKeys.length>0){
        this.addCategoryRuleForm.cat_pid = this.selectedKeys[this.selectedKeys.length-1]
        this.addCategoryRuleForm.cat_level = this.selectedKeys.length
        return
      }else {
        this.addCategoryRuleForm.cat_pid = 0
        this.addCategoryRuleForm.cat_level = 0
      }
      // console.log(this.addCategoryRuleForm);
    },
    //获取分类列表数据
    categoryHandle(data){
      CategoriesListData(data).then(res=>{
        if(res.meta.status==200){
          this.categoriesList = res.data.result
          this.total = res.data.total
        }
      })
    },
    //编辑控制
    categoryClcik(data){
      this.id = data.cat_id
      this.categoryRuleForm.cat_name = data.cat_name
      this.categoryDialogVisible=true
    },
    //更改分类
    submitClick(){
      CompileCategoryData({
        id:this.id,
        cat_name:this.categoryRuleForm.cat_name
      }).then(res=>{
        if(res.meta.status==200){
          this.categoryHandle(this.paramsInfo)
          this.$message.success(res.meta.msg)
        }
      })
      this.categoryDialogVisible = false
    },
    //确认添加分类
    addCategorySubmitClick(){
      addCategoryData(this.addCategoryRuleForm).then(res=>{
        if(res.meta.status==201){
          this.$message.success(res.meta.msg)
        }else{
          this.$message.error(res.meta.msg)
        }
      })
      this.categoryHandle(this.paramsInfo)
      this.addCategoryDialogVisible = false
    },
    //删除分类
    deleteClick(data){
      DeleteCategoryData({
        id:data.cat_id
      }).then(res=>{
        if(res.meta.status==200){
          this.categoryHandle(this.paramsInfo)
          this.$message.success(res.meta.msg)
        }
      })
    },
    //每页 ${val} 条
    handleSizeChange(val){
      this.paramsInfo.pagesize = val
      this.categoryHandle(this.paramsInfo)
    },
    //当前页
    handleCurrentChange(val){
      this.paramsInfo.pagenum = val
      this.categoryHandle(this.paramsInfo)
    }
  }
}
</script>

<style scoped lang="less">
  .el-cascader{
    width: 100%;
  }
</style>